package com.larissa.android.quiz.fragments;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupMenu;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.larissa.android.quiz.QuestionActivity;
import com.larissa.android.quiz.databinding.DetailedQuestionItemBinding;
import com.larissa.android.quiz.databinding.QuestionItemBinding;
import com.larissa.android.quiz.question.Question;
import com.larissa.android.quiz.R;
import com.larissa.android.quiz.question.QuestionRepository;

import java.util.List;

public class QuestionAdapter extends RecyclerView.Adapter<QuestionAdapter.ViewHolder> {

    private List<Question> questions;
    private final QuestionFragment fragment;
    private final boolean detailed;


    public QuestionAdapter(List<Question> questions, QuestionFragment fragment, boolean detailed) {
        this.questions = questions;
        this.fragment = fragment;
        this.detailed = detailed;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        if (detailed) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.detailed_question_item, parent, false);
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.question_item, parent, false);
        }
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Question question = questions.get(position);
        View.OnClickListener listener = v -> {
            if (fragment.getActivity() instanceof QuestionActivity) {
                QuestionActivity activity = (QuestionActivity) fragment.getActivity();
                int actionId;
                if (question.getCategory().equalsIgnoreCase(Question.GEO)) {
                    actionId = R.id.action_questionFragment_to_geoQuestionEditorFragment;
                } else {
                    actionId = R.id.action_questionFragment_to_mathQuestionEditorFragment;
                }
                Bundle bundle = new QuestionFragmentArgs.Builder(question).build().toBundle();
                activity.getNavHostFragment().getNavController().navigate(actionId, bundle);
            }
        };

        @SuppressLint("NotifyDataSetChanged") PopupMenu.OnMenuItemClickListener onMenuItemClickListener = item -> {
            if (item.getItemId() == R.id.menu_delete_question) {
                if (QuestionRepository.getInstance().getGeoQuestions().contains(question)) {
                    QuestionRepository.getInstance().getGeoQuestions().remove(question);
                } else if (QuestionRepository.getInstance().getMathQuestions().contains(question)) {
                    QuestionRepository.getInstance().getMathQuestions().remove(question);
                }
                QuestionRepository.getInstance().getQuestionsToBeDeleted().add(question);
                notifyDataSetChanged();
                return true;
            }
            return false;
        };

        if (detailed) {
            holder.getBindingDetailed().setQuestion(question);
            holder.getBindingDetailed().tvFragmentCategory.setOnClickListener(listener);
            holder.getBindingDetailed().tvFragmentText.setOnClickListener(listener);
            holder.getBindingDetailed().tvFragmentAnswer.setOnClickListener(listener);
            holder.getBindingDetailed().getRoot().setOnLongClickListener(v -> {
                PopupMenu menu = new PopupMenu(fragment.getContext(), v);
                menu.getMenuInflater().inflate(R.menu.menu_question_item, menu.getMenu());
                menu.setOnMenuItemClickListener(onMenuItemClickListener);
                menu.show();
                return true;
            });
        } else {
            holder.getBinding().setQuestion(question);
            holder.getBinding().tvFragmentText.setOnClickListener(listener);
            holder.getBinding().getRoot().setOnLongClickListener(v -> {
                PopupMenu menu = new PopupMenu(fragment.getContext(), v);
                menu.getMenuInflater().inflate(R.menu.menu_question_item, menu.getMenu());
                menu.setOnMenuItemClickListener(onMenuItemClickListener);
                menu.show();
                return true;
            });
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    public void setQuestions(List<Question> questions) {
        if (this.questions == questions)
            return;
        this.questions = questions;
        notifyDataSetChanged();
    }

    public boolean isDetailed() {
        return detailed;
    }

    @Override
    public int getItemCount() {
        return questions.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private QuestionItemBinding binding;
        private DetailedQuestionItemBinding bindingDetailed;

        public ViewHolder(@NonNull View view) {
            super(view);
            if (detailed) {
                bindingDetailed = DetailedQuestionItemBinding.bind(view);
            } else {
                binding = QuestionItemBinding.bind(view);
            }
        }

        public DetailedQuestionItemBinding getBindingDetailed() {
            return bindingDetailed;
        }

        public QuestionItemBinding getBinding() {
            return binding;
        }
    }

}
